/**
    入驻小区
**/
(function (vc) {
    var DEFAULT_PAGE = 1;
    var DEFAULT_ROWS = 10;
    vc.extends({
        data: {
            productStockManageInfo: {
                productStocks: [],
                total: 0,
                records: 1,
                moreCondition: false,
                barCode: '',
                prodName: '',
                conditions: {
                    productId: '',
                    valueId: '',
                    keyword: '',
                    barCode: '',
                    shopId: vc.getCurrentCommunity().shopId
                }
            }
        },
        _initMethod: function () {
            $that._initData();
            $that.productStockManageInfo.conditions.productId = vc.getParam('productId');
            $that.productStockManageInfo.conditions.valueId = vc.getParam('valueId');
            $that._listProducts();
            $that._listProductStocks(DEFAULT_PAGE, DEFAULT_ROWS);
        },
        _initEvent: function () {

            vc.on('productStockManage', 'listProduct', function (_param) {
                $that._listProductStocks(DEFAULT_PAGE, DEFAULT_ROWS);
            });
            vc.on('pagination', 'page_event', function (_currentPage) {
                $that._listProductStocks(_currentPage, DEFAULT_ROWS);
            });
        },
        methods: {
            _initData: function () {
                document.getElementById("enterBarCode").focus();
            },
            _listProductStocks: function (_page, _rows) {

                $that.productStockManageInfo.conditions.page = _page;
                $that.productStockManageInfo.conditions.row = _rows;
                var param = {
                    params: $that.productStockManageInfo.conditions
                };

                //发送get请求
                vc.http.apiGet('/productStock/queryProductStock',
                    param,
                    function (json, res) {
                        var _productStockManageInfo = JSON.parse(json);
                        $that.productStockManageInfo.total = _productStockManageInfo.total;
                        $that.productStockManageInfo.records = _productStockManageInfo.records;
                        $that.productStockManageInfo.productStocks = _productStockManageInfo.data;
                        vc.emit('pagination', 'init', {
                            total: $that.productStockManageInfo.records,
                            currentPage: _page
                        });
                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },

            _openDeleteProductStockModel: function (_product) {
                vc.emit('deleteProductStock', 'opendeleteProductStockModal', _product);
            },
            _queryProductMethod: function () {
                $that._listProductStocks(DEFAULT_PAGE, DEFAULT_ROWS);
            },
            _moreCondition: function () {
                if ($that.productStockManageInfo.moreCondition) {
                    $that.productStockManageInfo.moreCondition = false;
                } else {
                    $that.productStockManageInfo.moreCondition = true;
                }
            },
            _listProducts: function () {
                var param = {
                    params: {
                        page: 1,
                        row: 1,
                        productId: $that.productStockManageInfo.conditions.productId
                    }
                };
                //发送get请求
                vc.http.apiGet('/product.queryProduct',
                    param,
                    function (json, res) {
                        var _productManageInfo = JSON.parse(json);
                        let _product = _productManageInfo.data[0];

                        vc.copyObject(_product, $that.productStockManageInfo);
                        $that.productStockManageInfo.barCode = '';

                    }, function (errInfo, error) {
                        console.log('请求失败处理');
                    }
                );
            },
            _enterProductStock: function () {
                if ($that.productStockManageInfo.barCode == '') {
                    vc.toast('条形码不能为空');
                    return;
                }

                let param = {
                    shopId: vc.getCurrentCommunity().shopId,
                    barCode: $that.productStockManageInfo.barCode,
                    productId: $that.productStockManageInfo.conditions.productId,
                    valueId: $that.productStockManageInfo.conditions.valueId
                };
                vc.http.apiPost(
                    '/productStock/saveProductStock',
                    JSON.stringify(param),
                    {
                        emulateJSON: true
                    },
                    function (json, res) {
                        //vm.menus = vm.refreshMenuActive(JSON.parse(json),0);
                        let _json = JSON.parse(json);
                        $that.productStockManageInfo.barCode = '';
                        if (_json.code == 0) {
                            //关闭model
                            vc.toast('入库成功');
                            $that._listProductStocks(DEFAULT_PAGE, DEFAULT_ROWS);
                            return;
                        }

                        vc.toast(_json.msg);
                    },
                    function (errInfo, error) {
                        console.log('请求失败处理');

                        vc.toast(errInfo);
                    });

            },
            _goBack: function () {
                vc.goBack();
            }


        }
    });
})(window.vc);
